<?
//Page 0.1.6
class sym_page extends symbiont{
    public function ___main($design=''){
        global $kernel;
        $kernel->link->param2?$this->___page($design):$this->___map($design);
    }
    public function ___page($design=''){
        global $sql, $kernel;
        $page=$sql->select_each('pages', '*', array('name'=>$kernel->link->param2), '', '', 1);
        $d=new design('simples/'.$page['template']);
    }
    public function ___map($design=''){
        global $kernel, $sym;
        $kernel->symbiont_add('light_menu');
        $sym->light_menu->___main('pages', 0, 0, 0, '?', '?');
        $design=$this->_check($design, 'map');
        $d=new design($design, 'page', true);
    }
    public function ___add($design='', $parent=''){
        global $sql, $kernel;
        $design=$this->_check($design, 'add');
        $d=new design($design, 'page', false);
        
        $this->what='add';
        //Values
        $id=$sql->select_each('pages', 'id', array('name'=>$parent), '', '', 1);
            
        $this->val_name=!$id?$parent:'';
        $this->val_id='';
        $this->val_parent=$id?$id:'';
        $this->val_design='index';
        $this->val_access='0';
        $this->checked_home='';
        $this->checked_active='checked="checked"';
        $this->checked_hidden='';
        $this->checked_404='';
            
            
        $this->parents='';
        $this->selected='';
            
        $this->pages(0, '', 0, 0, 0);
            
        $this->labels='';
        $this->val_title='';
        $this->val_keywords='';
        $this->val_description='';
        $this->infos='';
        $this->languages='';
        
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->abbr=$val['abbr'];
            $this->lng_id=$val['id'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
        
    }
    public function ___edit($design='', $name=''){
        global $sql, $kernel;
        $design=$this->_check($design, 'add');
        $d=new design($design, 'page', false);
        if(isset($_POST['name'])){
            $name=data::word($_POST['name'], 'en', 0, '\$');
        }
        else{
            $name=data::word($name, 'en', 0, '\$');
        }
        
        //Take old values
        $this->what='edit';
        $this->val_name=$name;
        $val=$sql->select_each('pages', '*', array('name'=>$name), '', '', 1);
        if(!is_array($val)) return;
        $this->val_id=$val['id'];
        $this->val_parent=$val['parent_id'];
        $this->val_design=$val['template'];
        $this->val_access=$val['access_level'];
        $this->checked_home=$val['is_home']?'checked="checked"':'';
        $this->checked_active=$val['is_active']?'checked="checked"':'';
        $this->checked_hidden=$val['is_hidden']?'checked="checked"':'';
        $this->checked_404=$val['is_404']?'checked="checked"':'';
        
        
        $this->parents='';
        $this->selected='';
        
        $this->pages();
        
        $this->labels='';
        
        $this->languages='';
        $this->infos='';
        
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->abbr=$val['abbr'];
            $this->lng_id=$val['id'];
            
            $l=new label('pages.'.$name.'.title');
            $this->val_title=$l->get($val['id']);
            
            $l=new label('pages.'.$name.'.description');
            $this->val_description=$l->get($val['id']);
            
            $l=new label('pages.'.$name.'.keywords');
            $this->val_keywords=$l->get($val['id']);
            
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
    }
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete');
        $d=new design($design, 'page');
    }

    
    //DB functions
    public function ___db_add(){
        global $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])&&!isset($_POST['title'])&&!isset($_POST['keywords'])&&!isset($_POST['description'])&&!isset($_POST['design'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        if($_POST['name']==''){
            print('{"error":"Page name is empty."}');
            return;
        }
        global $sql, $kernel, $sym;
        
        $name=data::word($_POST['name'], 'en', 0, '\$');
        $home=isset($_POST['home'])?1:0;
        $active=isset($_POST['active'])?1:0;
        $hidden=isset($_POST['hidden'])?1:0;
        $p404=isset($_POST['404'])?1:0;
        $access_level=isset($_POST['access_level'])?data::number($_POST['access_level']):0;
        $template=data::file_system($_POST['design']);
        $parent=data::number($_POST['parent']);
        
        $tit=new label('pages.'.$name.'.title');
        foreach($_POST['title'] as $key=>$val){
            if(!$val) continue;
            $tit->add(data::html_view($val), $key);
        }
        $titile_lid=$tit->id;
        $kwd=new label('pages.'.$name.'.keywords');
        foreach($_POST['keywords'] as $key=>$val){
            if(!$val) continue;
            $kwd->add(data::html_view($val), $key);
        }
        $keywords_lid=$kwd->id;
        
        $dsc=new label('pages.'.$name.'.description');
        foreach($_POST['description'] as $key=>$val){
            if(!$val) continue;
            $dsc->add(data::html_view($val), $key);
        }
        $description_lid=$dsc->id;
        
        $id=$sql->insert('pages', array('name'=>$name, 'is_home'=>$home, 'is_active'=>$active, 'is_hidden'=>$hidden, 'is_404'=>$p404, 'access_level'=>$access_level, 'title_lid'=>$titile_lid, 'keywords_lid'=>$titile_lid, 'description_lid'=>$titile_lid, 'template'=>$template, 'parent_id'=>$parent));
        if(!$id){
            print('{"error":"Page adding error."}');
            return;
        }
        if($home) $sql->update('pages', array('is_home'=>0), '`id`!='.$id);
        if($p404) $sql->update('pages', array('404'=>0), '`id`!='.$id);
        
        print('{"ok":"Page added."}');
    }
    public function ___db_edit(){
        global $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])&&!isset($_POST['name'])&&!isset($_POST['title'])&&!isset($_POST['keywords'])&&!isset($_POST['description'])&&!isset($_POST['design'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        global $sql, $kernel, $sym;
        $id=data::number($_POST['id']);
        $name=data::word($_POST['name'], 'en', 0, '\$');
        $home=isset($_POST['home'])?1:0;
        $active=isset($_POST['active'])?1:0;
        $hidden=isset($_POST['hidden'])?1:0;
        $p404=isset($_POST['404'])?1:0;
        $access_level=isset($_POST['access'])?data::number($_POST['access']):0;
        $template=data::file_system($_POST['design']);
        $parent=data::number($_POST['parent']);
        
        $tit=new label('pages.'.$name.'.title');
        foreach($_POST['title'] as $key=>$val){
            if(!$val) $tit->del($key);
            else $tit->set(data::html_view($val), $key);
        }
        $titile_lid=$tit->id;
        
        $kwd=new label('pages.'.$name.'.keywords');
        foreach($_POST['keywords'] as $key=>$val){
            if(!$val) $kwd->del($key);
            else $kwd->set(data::html_view($val), $key);
        }
        $keywords_lid=$kwd->id;
        
        $dsc=new label('pages.'.$name.'.description');
        foreach($_POST['description'] as $key=>$val){
            if(!$val) $dsc->del($key);
            else $dsc->set(data::html_view($val), $key);
        }
        $description_lid=$dsc->id;
        
        $r=$sql->update('pages', array('name'=>$name, 'is_home'=>$home, 'is_active'=>$active, 'is_hidden'=>$hidden, 'is_404'=>$p404, 'access_level'=>$access_level, 'title_lid'=>$titile_lid, 'keywords_lid'=>$titile_lid, 'description_lid'=>$titile_lid, 'template'=>$template, 'parent_id'=>$parent), array('id'=>$id), 1);
        if(!$r){
            print('{"error":"Page editing error."}');
            return;
        }
        if($home) $sql->update('pages', array('is_home'=>0), '`id`!='.$id);
        if($p404) $sql->update('pages', array('404'=>0), '`id`!='.$id);
        print('{"ok":"Page edited."}');
    }
    public function ___db_delete(){
        global $user, $sql, $kernel;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $name=data::word($_POST['name'], 'en', 0, '\$');
        $page=$sql->select_each('pages', '*', array('name'=>$name), '', '', 1);
        $sub=$sql->count('pages', array('parent_id'=>$page['id']), 1);
        if($sub){
            print('{"error":"This category have subcategory."}');
            return;
        }
        else{
            global $kernel, $sym;
            if($page['title_lid']){
                $l=new label($page['title_lid']);
                $l->del();
            }
            if($page['description_lid']){
                $l=new label($page['description_lid']);
                $l->del();
            }
            if($page['keywords_lid']){
                $l=new label($page['keywords_lid']);
                $l->del();
            }
            $position=new positions('', 'page_'.$name);
            if(is_array($position->symbiont))
            foreach($position->symbiont as $val){
                if(!$val) continue;
                $s=design::symbiont_explode($val);
                $kernel->symbiont_add($s['symbiont']);
                $sym->$s['symbiont']->delete($s);
            }
            $position->del();
            $sql->delete('pages', array('id'=>$page['id']), 1);
            print('{"ok":"Category deleted."}');
            return;
        }
    }
    public function ___db_save($design){
        global $user, $sql, $kernel, $sym;
        if($user->access_level<9||!isset($_POST['json'])) return;
        foreach($_POST['json'] as $key=>$val){
            $title=data::symbiont($key);
            $id=$sql->select_each('positions', 'id', array('name'=>$title), '', '', 1);
            $sql->delete('positions', array('name'=>$title));
            foreach($val as $key=>$v){
                if(isset($v['symbiont'])&&isset($v['symbiont_new'])&&isset($v['access'])){
                    $symbiont=data::symbiont($v['symbiont']);
                    $symbiont_new=data::symbiont($v['symbiont_new']);
                    $access=data::number($v['access']);
                    $empty=$v['empty']=='true'?true:false;
                    //Delete
                    if($empty){
                        $s=design::symbiont_explode($symbiont);
                        $s_new=design::symbiont_explode($symbiont_new);
                        $kernel->symbiont_add($s['symbiont']);
                        $sym->$s['symbiont']->_delete($s, $s_new);
                    }
                    //Edit
                    elseif($symbiont&&$symbiont_new){
                        $s=design::symbiont_explode($symbiont);
                        $s_new=design::symbiont_explode($symbiont_new);
                        $kernel->symbiont_add($s['symbiont']);
                        $symbiont_after=$sym->$s['symbiont']->_edit($s, $s_new);
                    }
                    //Do nothing
                    elseif($symbiont){
                        $s=design::symbiont_explode($symbiont);
                        $kernel->symbiont_add($s['symbiont']);
                        $symbiont_after=$symbiont;
                    }
                    //Add
                    elseif($symbiont_new){
                        $s=design::symbiont_explode($symbiont_new);
                        $kernel->symbiont_add($s['symbiont']);
                        $symbiont_after=$sym->$s['symbiont']->_add($s);
                    }
                    if(isset($s)&&!$empty){
                        $values=array('symbiont'=>$symbiont_after, 'position'=>($key+1), 'access_level'=>$access);
                        if($id){
                            $values['id']=$id;
                        }
                        $values['name']=$title;
                        $sql->insert('positions', $values);
                    }
                }
            }
        }
    }
    
    //Json functions
    public function ___json_functions($design, $symbiont){
        global $kernel, $sym;
        $kernel->symbiont_add($symbiont);
        $methods=get_class_methods($sym->$symbiont);
        $array=array();
        foreach($methods as $val){
            if(substr($val, 0, 3)!='___') continue;
            $method=substr($val, 3);
            if(substr($method, 0, 2)!='db'&&substr($method, 0, 4)!='ajax'&&substr($method, 0, 4)!='json'){
                $array[]=$method;
            }
        }
        print json_encode($array);
    }
    public function ___json_page($design){
        global $sql;
        if(isset($_POST['page'])){
            $name=data::word($_POST['page']);
            if($sql->num_rows('pages', array('name'=>$name), 1)==0) print('{"result":true}');
            else print('{"result":false}');
        }
        else{
            print('{"result":false}');
        }
    }
    public function ___json_symbionts($design='', $name=''){
        global $sql, $kernel, $sym;
        if($name){
            $name=data::word($name, 'en', 0, '\.');
        }
        elseif(isset($_POST['position'])){
            $name=data::word($_POST['position'], 'en', 0, '\.');
        }
        else{
            return '[]';
        }
        $p=new positions('', $name);
        if(!is_array($p->symbiont)){
            print '[]'; return;
        }
        $positions=$sql->select_each('positions', array('symbiont', 'access_level', 'id'), array('name'=>$name), '', 'position');
        $r=array();
        if(is_array($positions)){
            $i=0;
            foreach($positions as $val){
                $r['symbiont'][$i]=$val['symbiont'];
                $r['access'][$i]=$val['access_level'];
                $s=design::symbiont_explode($val['symbiont']);
                $kernel->symbiont_add($s['symbiont']);
                $info=$sym->$s['symbiont']->_info($s);
                $r['title'][$i]=$info['title'];
                $r['block'][$i]=$info['block'];
                $i++;
            }
            $r['id']=$val['id'];
        }
        $r['name']=$name;
        print json_encode($r);
    }
    public function ___json_symbionts_all($design){
        $r=array();
        $dir=opendir("symbionts");
        while($file=readdir($dir)){
            if(($file!=".")&&($file!="..")&&($file[0]!='.')){
                $config='symbionts/'.$file.'/config.json';
                if(file_exists($config)){
                    $content=file_get_contents($config);
                    $json = json_decode($content);
                    if(isset($json->widget)&&$json->widget){
                        if(isset($json->title)){
                            $l=new label($json->title);
                            $title=$l->get();
                            
                        }
                        if(!isset($title)||$title==''){
                            $title=ucfirst(str_replace('_', ' ', $file));
                        }
                        $r[]=array('title'=>$title, 'symbiont'=>$file);
                    }
                }
            }
        }
        closedir($dir);
        print(json_encode($r));
    }
    
    //Private functions
    private function pages($id=0, $prefix='', $r=0, $g=0, $b=0, $s=true){
        if($r<=0) $r=rand(200, 255);
        if($g<=0) $g=rand(200, 255);
        if($b<=0) $b=rand(200, 255);
        global $sql, $kernel;
        $pages=$sql->select_each('pages', array('name', 'id'), array('parent_id'=>$id), '', 'name');
        if(is_array($pages)){
            foreach($pages as $key=>$val){
                if($val['name'][0]=='$') continue;
                $this->background='#'.$this->hex($r).$this->hex($g).$this->hex($b);
                $this->parent_name=$prefix.$val['name'];
                $this->parent_id=$val['id'];
                if($this->val_parent!=''&&$this->val_parent>=0) $this->selected=$val['id']==$this->val_parent?'selected="selected"':'';
                $this->parents.=design::run($this->parent_temp);
                if($this->parent_id!=0){
                    $this->pages($this->parent_id, '&nbsp;'.$prefix, $r-20, $g-20, $b-20, $s);
                }
            }
        }
    }
    private function hex($dec){
        $hex=dechex($dec);
        if(strlen($hex)==0) return '00';
        if(strlen($hex)==1) return '0'.$hex;
        return $hex;
    }
}
?>